﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;


namespace DAO
{
    public class DonViTinhDAO
    {
        #region 1.Retrieving
        public static List<DonViTinhDTO> SelectAllDonViTinh()
        {
            List<DonViTinhDTO> listDonViTinh = new List<DonViTinhDTO>();

            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectDonViTinhsAll");
            foreach (DataRow dr in dt.Rows)
            {
                DonViTinhDTO donVi = new DonViTinhDTO();
                donVi.MaDonViTinh = dr["MaDonViTinh"].ToString();
                donVi.TenDonViTinh = dr["TenDonViTinh"].ToString();
                donVi.TiGiaSangVND = float.Parse(dr["TiGiaSangVND"].ToString());

                listDonViTinh.Add(donVi);
            }

            return listDonViTinh;
        }
        public static bool CheckMaDonViTinhExits(string maDonViTinh)
        {
            List<DonViTinhDTO> listDonViTinh = new List<DonViTinhDTO>();

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaDonViTinh", maDonViTinh));
            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectDonViTinhsByAndMaDonViTinh", listSqlParam);
            foreach (DataRow dr in dt.Rows)
            {
                DonViTinhDTO donVi = new DonViTinhDTO();
                donVi.MaDonViTinh = dr["MaDonViTinh"].ToString();
                donVi.TenDonViTinh = dr["TenDonViTinh"].ToString();
                donVi.TiGiaSangVND = float.Parse(dr["TiGiaSangVND"].ToString());

                listDonViTinh.Add(donVi);
            }

            if (listDonViTinh.Count == 1)
            {
                return true;
            }
            else
                return false;
        }
        public static List<DonViTinhDTO> SelectDonViTinhByID(string maDonViTinh)
        {
            List<DonViTinhDTO> listDonViTinh = new List<DonViTinhDTO>();

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaDonViTinh", maDonViTinh));
            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectDonViTinhsByAndMaDonViTinh", listSqlParam);
            foreach (DataRow dr in dt.Rows)
            {
                DonViTinhDTO donVi = new DonViTinhDTO();
                donVi.MaDonViTinh = dr["MaDonViTinh"].ToString();
                donVi.TenDonViTinh = dr["TenDonViTinh"].ToString();
                donVi.TiGiaSangVND = float.Parse(dr["TiGiaSangVND"].ToString());

                listDonViTinh.Add(donVi);
            }

            return listDonViTinh;
        }
        #endregion
        #region 2.Inserting
        public static bool InsertDonViTinh(DonViTinhDTO loaiDaiLy)
        {

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaDonViTinh", loaiDaiLy.MaDonViTinh));
            listSqlParam.Add(new SqlParameter("@TenDonViTinh", loaiDaiLy.TenDonViTinh));
            listSqlParam.Add(new SqlParameter("@TiGiaSangVND", loaiDaiLy.TiGiaSangVND));

            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_InsertDonViTinh", listSqlParam);
            if (n == -1)
            {
                return true;
            }
            else
                return false;


        }
        #endregion
        #region 3.Updating
        public static bool UpdateDonViTinh(DonViTinhDTO donViTinh)
        {

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaDonViTinh", donViTinh.MaDonViTinh));
            listSqlParam.Add(new SqlParameter("@TenDonViTinh", donViTinh.TenDonViTinh));
            listSqlParam.Add(new SqlParameter("@TiGiaSangVND", donViTinh.TiGiaSangVND));

            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_UpdateDonViTinh", listSqlParam);
            if (n == -1)
            {
                return true;
            }
            else
                return false;

        }
        #endregion
        #region 4.Deleting
        public static bool DeleteDonViTinh(string maDonViTinh)
        {

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaDonViTinh", maDonViTinh));
            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_DeleteDonViTinh", listSqlParam);
            if (n == -1)
            {
                return true;
            }
            else
                return false;

        }
        #endregion
    }
}
